﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Events;
using JBeamLibrary.VehicleParts.Elements;
using JBeamLibrary.VehicleParts.Interfaces;

namespace JBeamLibrary.VehicleParts.Collections
{
    public class DifferentialCollection : Collection<Differential>, IVehicleGroupCollection<Differential>
    {
        #region Fields

        public event ElementPropertyChangedEventHandler ElementPropertyChanged;

        #endregion

        #region CTORs

        /// <summary>Creates a new differentials collection</summary>
        public DifferentialCollection() { }

        /// <summary>Creates a new differentials collection</summary>
        /// <param name="differentials"></param>
        public DifferentialCollection(IEnumerable<Differential> differentials)
        {
            AddRange(differentials);
        }

        #endregion

        #region Methods

        public new void Add(Differential differential)
        {
            base.Add(differential);

            differential.PropertyChanged += (s, e) =>
            {
                if (ElementPropertyChanged != null)
                    ElementPropertyChanged(s, e);
            };
        }

        public void Add(string wheel1, string wheel2)
        {
            Add(new Differential(wheel1, wheel2));
        }

        public void AddRange(IEnumerable<Differential> differentials)
        {
            foreach (var differential in differentials)
                Add(differential);
        }

        public void Remove(string wheel1, string wheel2)
        {
            for (var i = 0; i < Count; i++)
            {
                var differential = this[i];
                if (!differential.WheelName1.Equals(wheel1) || !differential.WheelName2.Equals(wheel2))
                    continue;

                RemoveAt(i);
                i--;
            }
        }

        public new void Remove(Differential element)
        {
            base.Remove(element);
        }

        #endregion
    }
}
